package com.ct.system.responsity.admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ct.repository.commons.BaseServiceImpl;
import com.ct.system.mapper.AdminSystemMapper;
import com.ct.system.mapper.AdminSystemUserMapper;
import com.ct.system.orm.AdminSystem;
import com.ct.system.orm.AdminSystemUser;
import com.ct.system.responsity.admin.IAdminSystemUserRepository;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 系统
 *
 * @author system
 */
@Repository
public class AdminSystemUserRepository  extends BaseServiceImpl<AdminSystemUserMapper, AdminSystemUser> implements IAdminSystemUserRepository {

    @Resource
    private AdminSystemMapper adminSystemMapper;

    @Override
    public List<AdminSystem> findAdminSystemAll(Long userId) {
        return adminSystemMapper.findAdminSystemByUserId(userId);
    }

    /**
     * 判断当前系统ID是否绑定
     * @param systemId
     * @return
     */
    @Override
    public boolean countSystemId(Long systemId){
        LambdaQueryWrapper<AdminSystemUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminSystemUser::getSystemId,systemId);
        return this.count(lambdaQueryWrapper) > 0;
    }

    /**
     * 判断用户ID是否绑定系统
     * @param userId
     * @return
     */
    @Override
    public boolean countUserId(Long userId){
        LambdaQueryWrapper<AdminSystemUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminSystemUser::getUserId,userId);
        return this.count(lambdaQueryWrapper) > 0;
    }

    @Override
    public boolean unbindSystemByUserId(Long userId,Long systemId) {
        LambdaQueryWrapper<AdminSystemUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminSystemUser::getUserId,userId);
        lambdaQueryWrapper.eq(AdminSystemUser::getSystemId,systemId);
        return this.remove(lambdaQueryWrapper);
    }

    @Override
    public boolean bindSystemByUserId(Long userId, Long systemId) {
        LambdaQueryWrapper<AdminSystemUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminSystemUser::getUserId,userId);
        lambdaQueryWrapper.eq(AdminSystemUser::getSystemId,systemId);
        long count = this.count(lambdaQueryWrapper);
        if(count == 0){
            AdminSystemUser adminSystemUser = new AdminSystemUser();
            adminSystemUser.setUserId(userId);
            adminSystemUser.setSystemId(systemId);
            this.saveOrUpdate(adminSystemUser);
            return true;
        }
        return false;
    }

}
